package net.javalib.gvmax.post.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Gvmax_post implements EntryPoint {
	/** AppState (login info / postUrl) */
	private AppState appState = null;
	/** Reference to server */
	private PostServiceAsync postService = GWT.create(PostService.class);

	/* (non-Javadoc)
	 * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
	 */
	public void onModuleLoad() {
		postService.login(GWT.getHostPageBaseURL(), new AsyncCallback<AppState>() {
			public void onSuccess(AppState result) {
				appState = result;
		        if(appState.isLoggedIn()) {
		        	loadApp();
		        } else {
		        	loadLogin();
		        }
		      }
		
			public void onFailure(Throwable error) {
				Window.alert("ERROR: "+error.getMessage());
	        	loadLogin();
			}
		
		});
	}
	
	/**
	 * Ask user to login.
	 */
	private void loadLogin() {
		Label loginLabel = new Label("Please sign in to your Google Account to access this application.");
		Anchor signInLink = new Anchor("Sign In");
		signInLink.setHref(appState.getLoginUrl());
		HorizontalPanel loginPanel = new HorizontalPanel();
		loginPanel.add(loginLabel);
		loginPanel.add(signInLink);
		RootPanel.get("content").add(loginPanel);
	}

	/**
	 * User is logged in start app.
	 */
	private void loadApp() {
		// Create UI
		VerticalPanel content = new VerticalPanel();
		content.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		content.add(new Label("You are currently logged in as : "+appState.getEmailAddress()));
		content.add(new Label("If this is not your Google Voice email, then logout and login again using your Google Voice email"));
		content.add(new Label("............................"));
		content.add(new Label("Please enter the URL you would like GVMax to forward your SMS and Voicemails to."));

		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(5);
		content.add(hp);

		final TextBox postUrl = new TextBox();
		if (appState.getPostUrl() != null)
			postUrl.setText(appState.getPostUrl());
		else
			postUrl.setText("");
		postUrl.setWidth("500px");
		hp.add(postUrl);

		Button button = new Button("SAVE");
		hp.add(button);
				
		Anchor signOutLink = new Anchor("Sign Out");
		signOutLink.setHref(appState.getLogoutUrl());
		content.add(signOutLink);
		
		RootPanel.get("content").add(content);
		
		// EVENTS
		
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				postService.setPostUrl(postUrl.getText(), new AsyncCallback<Void>() {
					public void onSuccess(Void result) {
						Window.alert("Post URL saved.");
					}
					public void onFailure(Throwable caught) {
						Window.alert("ERROR: "+caught.getMessage());
						caught.printStackTrace();
					}
				});
			}
		});
		
	}
}
